﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;

public partial class UserDefinedFunctions
{
    public static int GetPlayerAlliance(int id)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select AllianceID From AllianceMembers Where PlayerID = @n", conn))
            {
                cmd.Parameters.Add("@n", SqlDbType.Int).Value = id;
                object o = cmd.ExecuteScalar();
                if (o == null) return -1;
                int t;
                if (int.TryParse(o.ToString(), out t) == true)
                    return t;
                else
                    return -1;
            }
        }
    }

    public static string GetAllianceName(int id)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select Name From Alliances Where AllianceID = @n", conn))
            {
                cmd.Parameters.Add("@n", SqlDbType.Int).Value = id;
                return cmd.ExecuteScalar().ToString();
            }
        }
    }
    public static int GetAllianceId(string name)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select AllianceID From Alliances Where Name = @n", conn))
            {
                cmd.Parameters.Add("@n", SqlDbType.NVarChar).Value = name;
                object o = cmd.ExecuteScalar();
                if (o == null)
                    return -1;
                int t;
                if (int.TryParse(o.ToString(), out t) == true)
                    return t;
                else
                    return -1;
            }
        }
    }
    public static string  GetAlliancePassword(int id)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select Password From Alliances Where AllianceID = @n", conn))
            {
                cmd.Parameters.Add("@n", SqlDbType.Int).Value = id;
                return cmd.ExecuteScalar().ToString();
            }
        }
    }
};

